<?php
namespace app\wxapp\controller;

class User extends Base{

	//用户登录
	// http://192.168.200.99/equi/user/login
	// username
	// password
	// equi
	public function login(){



		$User = model("User");
		$res = array("code" => 0 ,"msg" => "登录成功","data" => "");
		if(request()->isPost()){
			$post = $data = input('');
			if(!isset($data) || !isset($data['password']) || !isset($data['username']) ){
				$res['code'] = 999;
				$res['msg'] = "请填写完整信息";
				die(myJSON($res));
			}else{
				//存在用户名密码检测是否正确
				$chRes = $User->loginCheck($data['username'],$data['password']);

				if( isset($chRes['code']) ){
					//检测失败返回信息
					$res = array_merge($res,$chRes);
					die(myJSON($res));
				}else{
					//检测成功，返回信息
					$res['data'] = nullToSpace($chRes);
					$User->where('openid' , $data['openid'] )->setField('openid','' );

					$User->where('user_id' , $chRes['user_id'] )->setField('openid',$post['openid'] );
// echo db()->getLastSql();
					die(myJSON($res));
				}
			}
		}
	}

	

	public function scancode(){

		$res = array("code" => 0 ,"msg" => "扫码登录成功","data" => "");
		$post = input('post.');
//print_r($post );
		if( !isset($post['equipment_id']) || !isset($post['qrcode']) || !isset($post['qrexptime']) || !isset($post['user_id']) ){
			$res['code'] = 2001;
			$res['msg'] = "参数不完整";
			die(myJSON($res));	
		}


		if ( ( $post['qrexptime'] - time() ) < 0 ){
			$res['code'] = 2002;
			$res['msg'] = "二维码已过期";
			die(myJSON($res));
		};

		$equi = model('equipment')->where('id',$post['equipment_id'])->find();

		if( $equi['qrcode'] != $post['qrcode']  ||  $equi['qrexptime'] != $post['qrexptime'] ){
			$res['code'] = 2003;
			$res['msg'] = "二维码验证错误";
			die(myJSON($res));
		}

		$rs = model('equipment')->where('id',$post['equipment_id'])->setField('qruserid',$post['user_id']);

		die(myJSON($res));
	}

}